<template>
  <div>
    <el-row :gutter="20" style="margin-bottom: 20px">
      <el-col :span="6">
        <el-select v-model="selectedGitInstance" placeholder="选择 Git 实例" clearable>
          <el-option v-for="item in gitInstances" :key="item" :label="item" :value="item" />
        </el-select>
      </el-col>
      <el-col :span="6">
        <el-input v-model="searchName" placeholder="搜索仓库名称" clearable />
      </el-col>
      <el-col :span="6">
        <el-button type="primary" @click="fetchRepos">查询</el-button>
      </el-col>
    </el-row>
    <el-table :data="repos" style="width: 100%" @sort-change="handleSortChange">
      <el-table-column prop="name" label="仓库名称" min-width="120" />
      <el-table-column prop="pathWithNamespace" min-width="200" label="路径" />
      <el-table-column prop="gitInstance" label="实例" min-width="80" align="center">
        <template #default="{ row }">
          <span v-if="row.gitInstance === 'beijing'">北京</span>
          <span v-else-if="row.gitInstance === 'chengdu'">成都</span>
          <span v-else>{{ row.gitInstance }}</span>
        </template>
      </el-table-column>
      <el-table-column label="本月" align="center">
        <el-table-column
          prop="commitCount"
          label="提交"
          align="right"
          min-width="60"
          sortable="custom"
          :sort-orders="['descending', 'ascending']"
        />
        <el-table-column
          prop="totalChanges"
          label="代码"
          align="right"
          min-width="60"
          sortable="custom"
          :sort-orders="['descending', 'ascending']"
        />
      </el-table-column>
      <el-table-column label="总计" align="center">
        <el-table-column
          prop="allCommitCount"
          label="提交"
          align="right"
          min-width="60"
          sortable="custom"
          :sort-orders="['descending', 'ascending']"
        />
        <el-table-column
          prop="allTotalChanges"
          label="代码"
          align="right"
          min-width="60"
          sortable="custom"
          :sort-orders="['descending', 'ascending']"
        />
      </el-table-column>
      <el-table-column prop="webUrl" min-width="200" label="Web URL">
        <template #default="{ row }">
          <a :href="row.webUrl" target="_blank" :title="row.webUrl">
            <span v-if="row.webUrl && row.webUrl.length > 36">
              {{ row.webUrl.slice(0, 20) }}...{{ row.webUrl.slice(-16) }}
            </span>
            <span v-else>
              {{ row.webUrl }}
            </span>
          </a>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
      style="margin-top: 20px; text-align: right"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { repositoryService } from '@/services/business/repository.service';
import type { Repository, RepoQueryParams } from '@/types/business/repository';
import type { GitInstance } from '@/types';

const repos = ref<Repository[]>([]);
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);
const selectedGitInstance = ref('');
const searchName = ref('');
const gitInstances = ref(['beijing', 'chengdu']); // 假设的 gitInstance 选项，根据实际调整
const sortBy = ref('totalChanges');
const sortDir = ref('DESC');

const fetchRepos = async () => {
  try {
    const params: RepoQueryParams = {
      page: currentPage.value,
      size: pageSize.value,
      gitInstance: selectedGitInstance.value as GitInstance,
      name: searchName.value,
      sortBy: sortBy.value,
      sortDir: sortDir.value as 'ASC' | 'DESC',
    };
    const response = await repositoryService.getRepositories(params);
    repos.value = response.records;
    total.value = response.total;
  } catch (error) {
    console.error('Error fetching repos:', error);
  }
};

const handleSizeChange = (val: number) => {
  pageSize.value = val;
  fetchRepos();
};

const handleCurrentChange = (val: number) => {
  currentPage.value = val;
  fetchRepos();
};

const handleSortChange = ({ prop, order }) => {
  if (order === null) {
    sortBy.value = 'totalChanges';
    sortDir.value = 'DESC';
  } else {
    sortBy.value = prop;
    sortDir.value = order === 'ascending' ? 'ASC' : 'DESC';
  }
  fetchRepos();
};

onMounted(() => {
  fetchRepos();
});
</script>
